package com.fg.pro.trade.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@Component
public class GmHttpUtils {

    @Lazy
    @Autowired
    GmTokenUtils gmTokenUtils;

    /**
     * 观麦htpp（无token）
     * @param url
     * @param obj
     * @param clazz
     * @return
     * @param <T>
     */
    public <T> T httpPost(String url, Object obj, Class<T> clazz) {
        JSONObject jsonData = JSONUtil.parseObj(obj);
        RestTemplate restTemplate = new RestTemplate();
        //设置Http Header
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("Accept","application/json, text/plain, */*");
        //设置请求媒体数据类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        //设置返回媒体数据类型
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
        String objRep = restTemplate.postForObject(url, formEntity, String.class);
//        Object bean = JSONUtil.toBean(objRep, clazz);
        GmReponse bean = JSONUtil.toBean(objRep, GmReponse.class);
        Object data = bean.getData();
        T t = BeanUtil.toBean(data, clazz);
        return t;
    }

    /**
     * 观麦htpp
     * @param url
     * @param obj
     * @param clazz
     * @return
     * @param <T>
     */
    public <U> U httpPostByToken(String url, Object obj, Class<U> clazz) throws InterruptedException {
        // h获取观麦token
        String token = gmTokenUtils.token();
        JSONObject jsonData = JSONUtil.parseObj(obj);
        RestTemplate restTemplate = new RestTemplate();
        //设置Http Header
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("Accept","application/json, text/plain, */*");
        headers.add("Access-Token",token);
        //设置请求媒体数据类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        //设置返回媒体数据类型
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
        String repStr = restTemplate.postForObject(url, formEntity, String.class);
        GmReponse repBean = JSONUtil.toBean(repStr, GmReponse.class);

        if(repBean.code.equals("200")){
            U data = JSONUtil.toBean(repBean.getData().toString(), clazz);
            return data;
        } else if (repBean.code.equals("54330013")) {
            gmTokenUtils.refreshToken();
            return httpPostByToken( url,  obj, clazz);
            //请求重复
        }else if (repBean.code.equals("10")) {
            TimeUnit.SECONDS.sleep(1);
            return httpPostByToken( url,  obj, clazz);
        }else {
            throw new RuntimeException(repBean.msg);
        }
    }

}
